package net.lovesrc.allin.jobs.backup;

import com.alibaba.fastjson.JSONObject;
import net.lovesrc.allin.biz.gt.GtService;
import net.lovesrc.allin.jobs.base.BaseJob;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

//@Component
@Profile("live")
public class GtAutoOpJyJob extends BaseJob {

    @Autowired
    private GtService gtService;

    @Override
    public void runOnce() {
        // 自动生成截图
        {
            List<JSONObject> rows = queryForRows(jdbcTemplate, "select * from t_gt_jy where status = 'Waiting_Buyer_Pay'");
            for (JSONObject row : rows) {
                Integer jyId = row.getInteger("jyId");
                Date updateTime = row.getDate("updateTime");
                if (new Date().getTime() - updateTime.getTime() >= DateUtils.MILLIS_PER_MINUTE * (4 + (jyId % 2))) {
                    gtService.jieTu(jyId, false);
                }
                if (new Date().getTime() - updateTime.getTime() >= DateUtils.MILLIS_PER_MINUTE * (6 + (jyId % 2))) {
                    gtService.payJyWithAutoJieTu(jyId);
                }
            }
        }

        // 自动确认交易
        {
            List<JSONObject> rows = queryForRows(jdbcTemplate, "select * from t_gt_jy where status = 'Waiting_Seller_Confirm'");
            for (JSONObject row : rows) {
                Integer jyId = row.getInteger("jyId");
                Date updateTime = row.getDate("updateTime");
                if (new Date().getTime() - updateTime.getTime() >= DateUtils.MILLIS_PER_MINUTE * (2 + (jyId % 3))) {
                    gtService.confirmJY(jyId);
                }
            }
        }
    }

    @Override
    protected long intervalMilliseconds() {
        return TimeUnit.SECONDS.toMillis(5);
    }

}
